package com.whfc.ms.api.safety.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.AssertUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.safety.service.MsSafetyRectifyService;
import com.whfc.ms.config.AbstractBaseController;
import com.whfc.safety.dto.SafetyCheckDTO;
import com.whfc.safety.dto.SafetyRectifyDTO;
import com.whfc.safety.param.SafetyRectifyAddParam;
import com.whfc.safety.param.SafetyRectifyEditParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author: hw
 * @date: 2021-10-19 16:16
 * @description: 安全整改单
 */
@RequiresPermissions("safety/rectify")
@RestController
@RequestMapping("/ms/api/safety/rectify")
public class SafetyRectifyController extends AbstractBaseController {

    @Autowired
    private MsSafetyRectifyService msSafetyRectifyService;

    @GetMapping("list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                       @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
                       @RequestParam(value = "pageNum") Integer pageNum,
                       @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("整改通知单列表,loginer:{},deptId:{},startTime:{},endTime:{},pageNum:{},pageSize{}",
                user.getUsername(), deptId, startTime, endTime, pageNum, pageSize);
        PageData<SafetyRectifyDTO> list = msSafetyRectifyService.list(deptId, startTime, endTime, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    @GetMapping("details")
    public Result details(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("rectifyId") Integer rectifyId,
                          @RequestParam(value = "pageNum") Integer pageNum,
                          @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("整改通知单详情,loginer:{},rectifyId:{},pageNum:{},pageSize{}",
                user.getUsername(), rectifyId, pageNum, pageSize);
        PageData<SafetyCheckDTO> list = msSafetyRectifyService.details(rectifyId, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    @RequiresPermissions("safety/rectify/add")
    @PostMapping("add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody SafetyRectifyAddParam param) {
        logger.info("添加整改通知单,loginer:{},param:{}", user.getUsername(), param.toString());
        param.setCreateUserId(user.getId());
        param.setCreateUserName(user.getNickname());
        msSafetyRectifyService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("safety/rectify/add")
    @PostMapping("edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody SafetyRectifyEditParam param) {
        logger.info("添加整改通知单,loginer:{},param:{}", user.getUsername(), param.toString());
        msSafetyRectifyService.edit(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("safety/rectify/del")
    @PostMapping("del/{rectifyId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("rectifyId") Integer rectifyId) {
        logger.info("删除整改通知单,loginer:{},rectifyId:{}", user.getUsername(), rectifyId);
        msSafetyRectifyService.del(rectifyId);
        return ResultUtil.success();
    }

    @GetMapping("export")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("rectifyId") Integer rectifyId) {
        logger.info("导出整改通知单,loginer:{},rectifyId:{}", user.getUsername(), rectifyId);
        msSafetyRectifyService.export(rectifyId);
        return ResultUtil.success();
    }

    /**
     * 2.0安全整改通知单批量删除
     *
     * @param idList
     * @return
     */
    @RequiresPermissions("safety/rectify/del")
    @PostMapping("/delete/batch")
    public R deleteBatch(@RequestBody List<Integer> idList) {
        logger.info("安全整改通知单批量删除,userId:{},idList:{}", getLoginUser().getId(), idList);
        AssertUtil.isTrue(CollectionUtils.isEmpty(idList), "ID不能为空");
        msSafetyRectifyService.deleteBatch(idList);
        return R.success();
    }
}
